﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using CsvHelper.Configuration;

namespace AIC_Notification
{
    public class StudentScore
    {
        public class StudentScoresGPA
        {
            public int Grade;
            public double GPA;
            public double GPA_Scale;
            public bool Weighted;
        }

        public class StudentScoreSAT2
        {
            public string Subject = "";
            public int Score = 0;
        }

        public class StudentScoreAP
        {
            public string Subject = "";
            public int Score = 0;
        }

        public string StudentID             { get; set; }
        public string StudentName           { get; set; }

        public double TOEFL_Listening  { get; set; }
        public double TOEFL_Reading    { get; set; }
        public double TOEFL_Speaking   { get; set; }
        public double TOEFL_Writing    { get; set; }
        public double TOEFL_Total      { get; set; }

        public double SAT_Reading      { get; set; }
        public double SAT_Math         { get; set; }

        public double ACT_English      { get; set; }
        public double ACT_Math         { get; set; }
        public double ACT_Reading      { get; set; }
        public double ACT_Science      { get; set; }
        public double ACT_Writing      { get; set; }

        public string GPA_Summary   { get; set; }

        public int SAT2_Count       { get; set; }
        public double SAT2_Mean     { get; set; }
        public string SAT2_Summary  { get; set; }
        public int AP_Count         { get; set; }
        public double AP_Mean       { get; set; }
        public string AP_Summary    { get; set; }

        // public IList<StudentScoresGPA> GPAs = null;
        public IList<StudentScoreSAT2> SAT2s = null;
        public IList<StudentScoreAP> APs = null;

        public StudentScore()
        {
            // GPAs = new List<StudentScoresGPA>();
            SAT2s = new List<StudentScoreSAT2>();
            APs = new List<StudentScoreAP>();
        }
/*
        public void PopulateGPA()
        {
            string GPAPattern = @"Grade\s+(?<Grade>[\d]+)\s*:\s*(?<GPA>[\d.]+)/(?<Scale>[\d.]+)\s*(?<Weighted>[W])?";

            MatchCollection GPAItems = Regex.Matches(GPA_Summary, GPAPattern);
            foreach (Match GPAItem in GPAItems)
            {
                StudentScoresGPA NewGPA = new StudentScoresGPA();

                NewGPA.Grade = Int32.Parse(GPAItem.Groups["Grade"].Value.Trim());
                NewGPA.GPA = Double.Parse(GPAItem.Groups["GPA"].Value.Trim());
                NewGPA.GPA_Scale = Double.Parse(GPAItem.Groups["Scale"].Value.Trim());
                NewGPA.Weighted = (GPAItem.Groups["Weighted"].Value == "W");

                GPAs.Add(NewGPA);
            }
        }
*/
        public void PopulateSAT2andAP()
        {
            string SubjectScorePattern = @"(?<Subject>[\w\s-]+)\s*:\s*(?<Score>[\d]+)";

            MatchCollection SAT2Items = Regex.Matches(SAT2_Summary, SubjectScorePattern);
            foreach (Match SAT2Item in SAT2Items)
            {
                StudentScoreSAT2 NewSAT2 = new StudentScoreSAT2();

                NewSAT2.Subject = SAT2Item.Groups["Subject"].Value.Trim();
                NewSAT2.Score = Int32.Parse(SAT2Item.Groups["Score"].Value);

                SAT2s.Add(NewSAT2);
            }

            MatchCollection APItems = Regex.Matches(AP_Summary, SubjectScorePattern);
            foreach (Match APItem in APItems)
            {
                StudentScoreAP NewAP = new StudentScoreAP();

                NewAP.Subject = APItem.Groups["Subject"].Value.Trim();
                NewAP.Score = Int32.Parse(APItem.Groups["Score"].Value);

                APs.Add(NewAP);
            }
        }

        public int SAT_Total()
        {
            return (int)SAT_Reading + (int)SAT_Math;
        }

        public int ACT_Composit()
        {
            double CompositScore = Math.Round(((double)ACT_English + (double)ACT_Math + (double)ACT_Reading + (double)ACT_Science) / 4.0);
            return (int)CompositScore;
        }
    }

    public sealed class StudentScoreMap : ClassMap<StudentScore>
    {
        public StudentScoreMap()
        {
            Map(m => m.StudentID).Index(0);
            Map(m => m.StudentName).Index(1);

            Map(m => m.TOEFL_Listening).Index(2);
            Map(m => m.TOEFL_Reading).Index(3);
            Map(m => m.TOEFL_Speaking).Index(4);
            Map(m => m.TOEFL_Writing).Index(5);
            Map(m => m.TOEFL_Total).Index(6);

            Map(m => m.SAT_Reading).Index(7);
            Map(m => m.SAT_Math).Index(8);

            Map(m => m.ACT_English).Index(9);
            Map(m => m.ACT_Math).Index(10);
            Map(m => m.ACT_Reading).Index(11);
            Map(m => m.ACT_Science).Index(12);
            Map(m => m.ACT_Writing).Index(13);

            Map(m => m.GPA_Summary).Index(14);

            Map(m => m.SAT2_Count).Index(15);
            Map(m => m.SAT2_Mean).Index(16);
            Map(m => m.SAT2_Summary).Index(17);
            Map(m => m.AP_Count).Index(18);
            Map(m => m.AP_Mean).Index(19);
            Map(m => m.AP_Summary).Index(20);
        }
    }
}
